package com.helloandroid

import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.FragmentActivity
import com.helloandroid.dialogs.PermissionNotifyDialog
import com.hjq.permissions.IPermissionInterceptor
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.PermissionFragment
import java.util.*

class PermissionInterceptor : IPermissionInterceptor {
    override fun requestPermissions(activity: FragmentActivity, callback: OnPermissionCallback, permissions: MutableList<String>) {

        if (permissions.indexOf(Permission.NOTIFICATION_SERVICE) >= 0) {
            // 通知权限
            PermissionNotifyDialog(activity) {
                PermissionFragment.beginRequest(activity, permissions as ArrayList<String>, callback)
            }.show()
        } else {
            // 这里的 Dialog 只是示例，没有用 DialogFragment 来处理 Dialog 生命周期
            AlertDialog.Builder(activity)
                    .setTitle("授权提示")
                    .setMessage("使用此功能需要先授予权限")
                    .setPositiveButton("授予") { dialog, _ ->
                        dialog.dismiss()
                        PermissionFragment.beginRequest(activity, permissions as ArrayList<String>, callback)
                    }
                    .setNegativeButton("取消") { dialog, _ -> dialog.dismiss() }
                    .show()
        }
    }
}